<?php

namespace app\admin\controller\file;

use app\admin\controller\AdminController;
use app\common\cache\ExportCache;
use think\response\Json;

class Download extends AdminController
{

    /**
     * 导出文件下载
     * @return \think\response\File|Json
     */
    public function export()
    {
        $fileKey = request()->get('file_key');

        $exportCache = new ExportCache();
        $fileInfo = $exportCache->getExportDownload($fileKey);
        if (empty($fileInfo)) return $this->error('下载文件不存在');
        $exportCache->delete($fileKey);

        header("Access-Control-Expose-Headers:Content-Disposition");
        return download( app()->getRootPath() . $fileInfo['url'], urlencode($fileInfo['name']));
    }

    /**
     * 导出文件进度
     * @return Json
     */
    public function exportProgress()
    {
        $fileIdentifier = request()->get('file_identifier');

        $exportCache = new ExportCache();
        $fileInfo = $exportCache->getExportFile($fileIdentifier);
        if (empty($fileInfo)) return $this->error('导出文件不存在');
        if($fileInfo['progress'] == 100 && $fileInfo['file_key'] && $fileInfo['step'] == 4)$exportCache->delete($fileIdentifier);

        return $this->success($fileInfo);
    }
}